package cn.fan.rabbitMQ.listener;

import cn.fan.pojo.FundMain;
import cn.fan.service.FundDataService;
import cn.fan.service.FundMainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class FundListener {

    @Autowired
    private FundMainService fundMainService;

    @Autowired
    private FundDataService fundDataService;

    @RabbitListener(queues = "error.queue")
    public void error(String msg){
        log.error("死信队列"+msg);
    }

    @RabbitListener(queues = "fund.queue")
    public void fundDirect(String msg){
        log.info("开始全量同步数据 "+msg);
        List<FundMain> fundMainList = fundMainService.list();
        if (fundMainList.size()==0){
            log.info("无数据，无需同步！");
            return;
        }

        String codes = "";
        for (FundMain fundMain:fundMainList) {
            codes += fundMain.getCode()+",";
        }
        codes = codes.substring(0,codes.length()-1); // 去掉最后一个,号

        final String codesStr=codes;
        Thread t1= new Thread(){
            public void run(){
                fundDataService.synchroAll(codesStr);
                log.info("所有数据同步完成");
            }
        };
        t1.start();

        log.info("后台全量同步中！");
    }

    @RabbitListener(queues = "synchroAll.queue")
    public void synchroAll(String msg){
        log.info("msg:"+msg);
    }
}
